﻿import flash.geom.Point;
import flash.geom.Matrix;

class com.embedchat.skins.standart.window.WindowClass extends MovieClip{
	static var instanceName:String = new String ("WindowClass");
	static var instanceOwner:Object = com.embedchat.skins.standart.window.WindowClass;

	public var canvasWidth:Number;
	public var canvasHeight:Number;
	
	public var broadcasterObj:Object = new Object();
	public var messagesListenerObj:Object = new Object();
	
	public var buttonWidth:Number = 16;
	public var buttonHeight:Number = 16;
	
	private var fontSize:Number = 10;

	public var verticalScrollBar:MovieClip = null;
	public var textField:TextField = null;
	
	public var postScrollCheckInterval:Number = -1;
	public var skipScrollbarSinhronization:Boolean = false;
	//constructor
	function WindowClass(){
		AsBroadcaster.initialize(this.broadcasterObj); 
		this.broadcasterObj.addListener(this.messagesListenerObj); 
		this.messagesListenerObj.onScrollButtonPress = this.onScrollButtonPress;
		this.messagesListenerObj.onScrollSliderPress = this.onScrollSliderPress;
		this.messagesListenerObj.onScrollSliderMove = this.onScrollSliderMove;		
		this.messagesListenerObj.onScrollSliderRelease = this.onScrollSliderRelease;				
		this.messagesListenerObj.onSinhronizeScroll = this.onSinhronizeScroll;						
		
		this.drawWrapper();
		this.textField = this.initTextField("textField");
		this.setInitalTextFieldFormat(this.textField);
		//this.textField.text =";
		
	}
	
	//wrapper for textfield & scrollbar
	private function drawWrapper():Void{
		var fillType:String = "linear"
		var colors:Array = [0xBCD0E8,0xFFFFFF];
		var alphas:Array = [20, 100];
		var ratios:Array = [0x00, 0xFF];
		var matrix:Matrix = new Matrix();
		matrix.createGradientBox(this.canvasWidth, this.canvasHeight, 0, 0, 0);
		var spreadMethod:String = "pad";
			
		this.lineStyle(0, 0xBCD0E8, 100, true, "none", "round", "miter", 1);		
		this.beginGradientFill(fillType, colors, alphas, ratios, matrix, spreadMethod); 
 
		this.moveTo(0,0);
		this.lineTo(this.canvasWidth,0);
		this.lineTo(this.canvasWidth,this.canvasHeight);
		this.lineTo(0,this.canvasHeight);		
		this.lineTo(0,0);
		this.endFill();
	}
	
	private function initTextField(fieldName:String):TextField{
		var tF:TextField = this.createTextField(fieldName,this.getNextHighestDepth(),0,0,this.canvasWidth,this.canvasHeight);
		tF.text = "";
		tF.onScroller = function(){
			if(this.maxscroll > 1){
				if(_parent.verticalScrollBar == null){
					_parent.verticalScrollBar = _parent.initScrollBar("verticalScrollBar","vertical",1,_parent.messagesObj);
					this._width = _parent.canvasWidth - _parent.buttonWidth;
				}
			}else{
				if(_parent.verticalScrollBar){
					_parent.verticalScrollBar.removeMovieClip();
					_parent.verticalScrollBar = null;
					this._width = _parent.canvasWidth;					
				}
			}
			if(_parent.verticalScrollBar && !_parent.skipScrollbarSinhronization){
				_parent.verticalScrollBar.updateTextVerticalScrollbar(this);
			}
			if(_parent.postScrollCheckInterval !=-1) clearInterval(_parent.postScrollCheckInterval);
			_parent.postScrollCheckInterval = setInterval(_parent,"postScrollCheck",50,{oldScrollValue:this.scroll});
		};
		return tF;
	}
	
	private function postScrollCheck(inputData:Object):Void{
		clearInterval(this.postScrollCheckInterval);
		var oldScrollValue:Number = inputData.oldScrollValue;
		if(oldScrollValue != this.textField.scroll){
			this.textField.onScroller();
		}
	}
	
	private function initScrollBar(movieName:String, direction:String, scrollBarCount:Number, broadcasterObj:Object):MovieClip{
		var start:Point;
		var end:Point;		
		switch(direction){
			case "vertical":
				if(scrollBarCount == 1){
					start = new Point((this.canvasWidth-this.buttonWidth),0);
					end = new Point(this.buttonWidth,this.canvasHeight);					
				}else if(scrollBarCount == 2){
					start = new Point((this.canvasWidth-this.buttonWidth),0);
					end = new Point(this.buttonWidth,this.canvasHeight-this.buttonHeight);					
				}
			break;
			case "horizontal":
				if(scrollBarCount == 1){
					start = new Point(0,(this.canvasHeight-this.buttonHeight));
					end = new Point(this.canvasWidth,this.buttonHeight);					
				}else if(scrollBarCount == 2){
					start = new Point(0,(this.canvasHeight-this.buttonHeight));
					end = new Point(this.canvasWidth-this.buttonWidth,this.buttonHeight);					
				}
			break;
			break;
			default:
				return null;
			break;
		}
		return this.attachMovie("ScrollBar",movieName,this.getNextHighestDepth(),{direction:direction,canvasWidth:end.x,canvasHeight:end.y, _x:start.x, _y:start.y, buttonWidth:this.buttonWidth, buttonHeight:this.buttonHeight, broadcasterObj:this.broadcasterObj});
	}
	
	//custom event handler  	
	private function onScrollButtonPress(sender:MovieClip):Void{
		var rootMovie:MovieClip = sender._parent._parent;
		if(rootMovie.textField){
			switch(sender.arrawDirection){
				case "up":
					rootMovie.textField.scroll--;
				break;
				case "down":
					rootMovie.textField.scroll++;
				break;
				case "left":
				break;
				case "right":
				break;
			}
		}
	}
	
	//custom event handler  
	private function onScrollSliderPress(sender:MovieClip):Void{
		var rootMovie:MovieClip = sender._parent._parent;
		if(rootMovie.textField){
			switch(sender.direction){
				case "vertical":
					rootMovie.verticalScrollBar.setSliderPosition(new Point(rootMovie._xmouse,rootMovie._ymouse));
					rootMovie.skipScrollbarSinhronization=true;
				break;
				case "horizontal":
				break;
			}
		}
	}
	
	//custom event handler  
	private function onScrollSliderMove(sender:MovieClip, mouse:Point):Void{
		var rootMovie:MovieClip = sender._parent._parent;
		if(rootMovie.textField){
			switch(sender.direction){
				case "vertical":
					rootMovie.verticalScrollBar.updateVerticalSlider(new Point(rootMovie._xmouse,rootMovie._ymouse));
				break;
				case "horizontal":
				break;
			}
		}
	}
	//custom event handler  	
	private function onScrollSliderRelease(sender:MovieClip, mouse:Point):Void{
		var rootMovie:MovieClip = sender._parent._parent;
		if(rootMovie.textField){
			switch(sender.direction){
				case "vertical":
					rootMovie.skipScrollbarSinhronization=false;
				break;
				case "horizontal":
				break;
			}
		}
	}
	//custom event handler  
	private function onSinhronizeScroll(sender:MovieClip, scrollRealSizeBefore:Point, scrollRealSize:Point,sliderSize:Point):Void{
		var rootMovie:MovieClip = sender._parent;
		if(rootMovie.textField){
			switch(sender.direction){
				case "vertical":
					rootMovie.textField.scroll = Math.floor((rootMovie.textField.maxscroll)*scrollRealSizeBefore.y/(scrollRealSize.y-sliderSize.y));
				break;
				case "horizontal":
				break;
			}
		}
	}
	
	private function setInitalTextFieldFormat(textField:TextField):Void{
		var textFieldFormat:TextFormat = new TextFormat();
		textFieldFormat.font = "Verdana";
		textFieldFormat.size = this.fontSize;
		textFieldFormat.bold = true;
		textField.multiline = true;
		textField.wordWrap = true;
		textField.type = "dynamic";
		textField.setNewTextFormat(textFieldFormat);
	}
	
	//public
	public function setTextFieldType(type:String):Void{
		this.textField.type = type;
	}
	public function addTextFieldText(text:String):Void{
		this.textField.text = this.textField.text + text;
	}
	
	public function scrollDown():Void{
		this.textField.scroll = this.textField.maxscroll;
	}

}